<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page contentType="text/html" pageEncoding="windows-1252"%>

<%@page import="java.lang.management.*, java.util.*" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!-- see http://www.freshblurbs.com/explaining-java-lang-outofmemoryerror-permgen-space -->
<html>
   <head>
      <title>JVM Memory Monitor</title>
      <style type="text/css">
         td { text-align: right; }
      </style>
   </head>
   <%
      final float mbFac = 1024 * 1024;
      MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();

      List<MemoryPoolMXBean> poolBeans = ManagementFactory.getMemoryPoolMXBeans();
      Runtime runtime = Runtime.getRuntime();
   %>
   <body>
      <h3>Runtime Memory</h3>
      <table border="1" cellpadding="2" cellspacing="0" width="100%">
         <tr>
            <th>Free</th>
            <th>Max</th>
            <th>Total</th>
         </tr>
         <tr>
            <td><fmt:formatNumber value="<%= runtime.freeMemory() / mbFac%>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
            <td><fmt:formatNumber value="<%= runtime.maxMemory() / mbFac%>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
            <td><fmt:formatNumber value="<%= runtime.totalMemory() / mbFac%>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
         </tr>
      </table>

      <h3>Total Memory</h3>
      <table border="1" cellpadding="2" cellspacing="0" width="100%">
         <tr>
            <th>usage</th>
            <th>init</th>
            <th>used</th>
            <th>committed</th>
            <th>max</th>
         </tr>
         <tr>
            <td style="text-align: left">Heap Memory Usage</td>
            <td><fmt:formatNumber value="<%= memoryBean.getHeapMemoryUsage().getInit() / mbFac %>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
            <td><fmt:formatNumber value="<%= memoryBean.getHeapMemoryUsage().getUsed() / mbFac %>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
            <td><fmt:formatNumber value="<%= memoryBean.getHeapMemoryUsage().getCommitted() / mbFac %>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
            <td><fmt:formatNumber value="<%= memoryBean.getHeapMemoryUsage().getMax() / mbFac %>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
         </tr>
         <tr>
            <td style="text-align: left">Non-heap Memory Usage</td>
            <td><fmt:formatNumber value="<%= memoryBean.getNonHeapMemoryUsage().getInit() / mbFac%>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
            <td><fmt:formatNumber value="<%= memoryBean.getNonHeapMemoryUsage().getUsed() / mbFac %>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
            <td><fmt:formatNumber value="<%= memoryBean.getNonHeapMemoryUsage().getCommitted() / mbFac %>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
            <td><fmt:formatNumber value="<%= memoryBean.getNonHeapMemoryUsage().getMax() / mbFac %>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
         </tr>
      </table>

      <h3>Memory Pools</h3>
      <table border="1" cellpadding="2" cellspacing="0" width="100%">
         <tr>
            <th>name</th>
            <th>usage</th>
            <th>init</th>
            <th>used</th>
            <th>committed</th>
            <th>max</th>
         </tr>
         <% for (MemoryPoolMXBean bean: poolBeans) { %>
            <tr>
               <td style="text-align: left"><%= bean.getName() %></td>
               <td style="text-align: left">Memory Usage</td>
               <td><fmt:formatNumber value="<%= bean.getUsage().getInit() / mbFac %>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
               <td><fmt:formatNumber value="<%= bean.getUsage().getUsed() / mbFac%>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
               <td><fmt:formatNumber value="<%= bean.getUsage().getCommitted() / mbFac%>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
               <td><fmt:formatNumber value="<%= bean.getUsage().getMax() / mbFac%>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
            </tr>
            <tr>
               <td></td>
               <td style="text-align: left">Peak Usage</td>
               <td><fmt:formatNumber value="<%= bean.getPeakUsage().getInit() / mbFac %>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
               <td><fmt:formatNumber value="<%= bean.getPeakUsage().getUsed() / mbFac%>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
               <td><fmt:formatNumber value="<%= bean.getPeakUsage().getCommitted() / mbFac%>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
               <td><fmt:formatNumber value="<%= bean.getPeakUsage().getMax() / mbFac%>" minFractionDigits="2" maxFractionDigits="2"/> MB</td>
            </tr>
         <% } %>
      </table>

   </body>
</html>